#include <upower.h>
#include <stdio.h>

static void ac_online_changed_cb(GObject *obj,  GParamSpec *pspec, gpointer data) {
    gboolean online;
    const gchar *pname =   g_param_spec_get_name(pspec);
    printf("Param %s changed!\n", pname);
    if(g_strcmp0(pname, "online")==0) {
        g_object_get(obj, pname, &online, NULL);
        printf("\tnew value: %d\n", online);
    }
}

int main(void) {
    UpDeviceKind ukind;
    UpDeviceState ustate;
	UpClient *upc = up_client_new();
    GPtrArray *dary = up_client_get_devices(upc);
    UpDevice *dev, *dev_bat, *dev_ac;
    char *npath; // /sys/class/.../<npath>/
    int i;
    for(i=0; i<dary->len; i++) {
        dev = g_ptr_array_index(dary, i);
        g_object_get(dev, "kind", &ukind, "state", &ustate, "native-path", &npath, NULL);
        printf("Dev: %s, kind:%d, state: %d\n", npath, ukind, ustate);
        if(ukind==UP_DEVICE_KIND_BATTERY) dev_bat=dev;
        else if(ukind==UP_DEVICE_KIND_LINE_POWER && g_strcmp0(npath, "usb")) 
            dev_ac = dev;
    }
    g_signal_connect(G_OBJECT(dev_ac), "notify::online", G_CALLBACK(ac_online_changed_cb), NULL);
    //GMainContext *ctx = g_main_context_new();
    GMainLoop *loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);
    return 0;
}
